<?php

namespace app\service\impl;

use app\popo\model\Dish;
use app\popo\model\Order;
use app\popo\model\Setmeal;
use app\popo\model\User;
use app\popo\vo\BusinessDataVO;
use app\popo\vo\DishOverViewVO;
use app\popo\vo\OrderOverViewVO;
use app\popo\vo\SetmealOverViewVO;
use app\service\WorkspaceService;
use Carbon\Carbon;

class WorkspaceServiceImpl implements WorkspaceService
{
    /**
     * 根据时间段统计营业数据
     * @param string $begin 开始时间
     * @param string $end 结束时间
     * @return BusinessDataVO
     */
    public function getBusinessData(string $begin, string $end): BusinessDataVO
    {
        // 营业额：当日已完成订单的总金额
        $turnover = Order::where('order_time', '>=', $begin)
            ->where('order_time', '<=', $end)
            ->where('status', 5) // 已完成
            ->sum('amount');

        // 有效订单数：当日已完成订单数量
        $validOrderCount = Order::where('order_time', '>=', $begin)
            ->where('order_time', '<=', $end)
            ->where('status', 5) // 已完成
            ->count();

        // 总订单数：当日所有订单数量
        $totalOrderCount = Order::where('order_time', '>=', $begin)
            ->where('order_time', '<=', $end)
            ->count();

        // 订单完成率
        $orderCompletionRate = $totalOrderCount > 0 ? round($validOrderCount / $totalOrderCount, 2) : 0.0;

        // 平均客单价
        $unitPrice = $validOrderCount > 0 ? round($turnover / $validOrderCount, 2) : 0.0;

        // 新增用户数：当日注册的用户数量
        $newUsers = User::where('create_time', '>=', $begin)
            ->where('create_time', '<=', $end)
            ->count();

        // 封装到BusinessDataVO
        $businessDataVO = new BusinessDataVO();
        $businessDataVO->setTurnover($turnover ?: 0.0);
        $businessDataVO->setValidOrderCount($validOrderCount);
        $businessDataVO->setOrderCompletionRate($orderCompletionRate);
        $businessDataVO->setUnitPrice($unitPrice);
        $businessDataVO->setNewUsers($newUsers);

        return $businessDataVO;
    }

    /**
     * 查询订单管理数据
     * @return OrderOverViewVO
     */
    public function getOrderOverView(): OrderOverViewVO
    {
        // 待接单：状态为2
        $waitingOrders = Order::where('status', 2)->count();

        // 待派送：状态为3（已接单）
        $deliveredOrders = Order::where('status', 3)->count();

        // 已完成：状态为5
        $completedOrders = Order::where('status', 5)->count();

        // 已取消：状态为6
        $cancelledOrders = Order::where('status', 6)->count();

        // 全部订单
        $allOrders = Order::count();

        // 封装到OrderOverViewVO
        $orderOverViewVO = new OrderOverViewVO();
        $orderOverViewVO->setWaitingOrders($waitingOrders);
        $orderOverViewVO->setDeliveredOrders($deliveredOrders);
        $orderOverViewVO->setCompletedOrders($completedOrders);
        $orderOverViewVO->setCancelledOrders($cancelledOrders);
        $orderOverViewVO->setAllOrders($allOrders);

        return $orderOverViewVO;
    }

    /**
     * 查询菜品总览
     * @return DishOverViewVO
     */
    public function getDishOverView(): DishOverViewVO
    {
        // 已启售：状态为1
        $sold = Dish::where('status', 1)->count();

        // 已停售：状态为0
        $discontinued = Dish::where('status', 0)->count();

        // 封装到DishOverViewVO
        $dishOverViewVO = new DishOverViewVO();
        $dishOverViewVO->setSold($sold);
        $dishOverViewVO->setDiscontinued($discontinued);

        return $dishOverViewVO;
    }

    /**
     * 查询套餐总览
     * @return SetmealOverViewVO
     */
    public function getSetmealOverView(): SetmealOverViewVO
    {
        // 已启售：状态为1
        $sold = Setmeal::where('status', 1)->count();

        // 已停售：状态为0
        $discontinued = Setmeal::where('status', 0)->count();

        // 封装到SetmealOverViewVO
        $setmealOverViewVO = new SetmealOverViewVO();
        $setmealOverViewVO->setSold($sold);
        $setmealOverViewVO->setDiscontinued($discontinued);

        return $setmealOverViewVO;
    }
}